import sys
sys.path.append('C:/Users/Hu/Dropbox/Research/PythonWork/Cancer/src/STAT/')

import numpy as np
import csv
from datetime import datetime
from time import strftime
import matplotlib.pyplot as plt
import math
import random
from scipy import stats
import ols

'''
Global and Local Empirical Bayes Smoothers with Gamma Model
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    f = open(inputCSV)
    #ra = csv.DictReader(file(fn), dialect="excel")
    ra = csv.DictReader(f, dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = int(float(record[item]))
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print '===================================================='
    print "begin at " + getCurTime()

    filepath = 'C:/_DATA/migration_census_2000/REDCAPLUS_100Region/'
    file = filepath + 'census_migration_100regs_dis.csv'
    data = build_data_list(file)

    #inoutflowfile = filePath + 'census_county_migration_aggregation_inoutflow.csv'    #[inflow, outflow]
    inoutflowfile = filepath + 'census_migration_100regs_inoutflow.csv'
    inoutflow = build_data_list(inoutflowfile)

    x = []
    for item in data:
        x.append([1, np.log(float(inoutflow[item[1],1])*inoutflow[item[2],0]), np.log(item[0])])
    x = np.array(x)
    x.shape = (-1, 3)

    #x = np.array(zip(x1, np.log(data[:,0])))
    #x.shape = (-1, 3)
    y = np.log(data[:,-1])

    s_hat = []
    
    #print x
    #fileLoc = filepath + 'piecewiseR_model2_results.csv'
    fileLoc = filepath + 'separateR_model2_results.csv'
    #np.savetxt(fileLoc, result, delimiter=',', fmt = '%s')
    